package com.jbinfo.mongoot.controller;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.jbinfo.mongoot.operation.Volunteer.Volunteer;
import com.jbinfo.mongoot.util.PageModel;
import com.jbinfo.mongoot.util.XiMethodUtil;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;

@RestController
@RequestMapping("/volunteer")
@Api("ejiaqin-volunteer类API")
public class VolunteerController {
	@Autowired
	private MongoTemplate mongoTemplate;
	
	
	/**
	 * @author liuxiaoxi
	 * @time 2017年3月25日
	 * @param volunteer
	 * @return void
	 * @throws ParseException 
	 * @describe 增
	 */
	@ApiOperation("Add~~~~~~~~~~~~~~~~~~~~~~~~~~~~测试成功")
	@RequestMapping(value = "/VLAdd", method = RequestMethod.POST)
	public Map<String, Object> VLAdd(@ModelAttribute Volunteer volunteer) throws ParseException {
		if (null!=volunteer.getUid()&&volunteer.getUid()!="") {
			Map<String, Object> vlUpdate = VLUpdate(volunteer);
			return vlUpdate;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		if (null==volunteer.getNumberid()||volunteer.getNumberid().length()==0) {
			map.put("status", 0);
			map.put("code", "");
			map.put("description", "请完善用户信息");
			return map;
		}else {
			Date date = new Date();
			volunteer.setCreatetime(date);//创建时间
			volunteer.setUpdatetime(date);//修改时间
			volunteer.setInvalid("1");//正常
			String uid = UUID.randomUUID().toString();
			volunteer.setUid(uid);
			mongoTemplate.insert(volunteer);
			map.put("status", 1);
			map.put("code", "");
			map.put("description", "添加成功");
			return map;
		}
    }
	
	/**
	 * @author liuxiaoxi
	 * @time 2017年3月25日
	 * @param id
	 * @return void
	 * @describe 数据库删除指定的某条记录
	 */
	@ApiOperation("Delete~~~~~~~~~~~~~~~~~~~~~~~~~~~~测试成功")
	@RequestMapping(value = "/VLDelete", method = RequestMethod.POST)
	public Map<String, Object> VLDelete(@RequestParam("uid") String uid) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (null==uid) {
			map.put("status", 0);
			map.put("code", "");
			map.put("description", "删除失败");
			return map;
		}
		DBCollection collection = mongoTemplate.getCollection(mongoTemplate.getCollectionName(Volunteer.class));
		DBObject query =new BasicDBObject();
		query.put("uid", uid);
		collection.remove(query);
		map.put("status", 1);
		map.put("code", "");
		map.put("description", "删除成功");
		return map;
    }
	
	/**
	 * @author liuxiaoxi
	 * @time 2017年3月25日
	 * @param id
	 * @return void
	 * @describe 数据库删除指定的某条记录
	 */
	@ApiOperation("DeleteF~~~~~~~~~~~~~~~~~~~~~~~~~~~~测试成功")
	@RequestMapping(value = "/VLDeleteF", method = RequestMethod.POST)
	public Map<String, Object> VLDeleteF(@RequestParam("uid") String uid) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (null==uid||uid.length()==0) {
			map.put("status", 0);
			map.put("code", "");
			map.put("description", "删除失败");
			return map;
		}
		DBCollection collection = mongoTemplate.getCollection(mongoTemplate.getCollectionName(Volunteer.class));
		DBObject query =new BasicDBObject();
		query.put("uid", uid);
		DBObject update = collection.findOne(query);//找到要修改的对象
		update.put("invalid", "0");//将数据修改为逻辑删除
		collection.update(query, update, false, true);//有的话修改，没有的话不进行添加！第三个参数为false。
		map.put("status", 1);
		map.put("code", "");
		map.put("description", "删除成功");
		return map;
    }
	
	/**
	 * @author liuxiaoxi
	 * @time 2017年3月25日
	 * @param key,volunteer
	 * @return void
	 * @describe 多条件修改一条数据
	 */
	@ApiOperation("update~~~~~~~~~~~~~~~~~~~~~~~~~~~~测试成功")
	@RequestMapping(value = "/VLUpdate", method = RequestMethod.POST)
	public Map<String, Object> VLUpdate(@ModelAttribute Volunteer volunteer) throws ParseException {
		Map<String, Object> map = new HashMap<String, Object>();
		if (null==volunteer||volunteer.getUid().length()==0) {
			map.put("status", 0);
			map.put("code", "");
			map.put("description", "请填写信息");
			return map;
		}
		DBCollection collection = mongoTemplate.getCollection(mongoTemplate.getCollectionName(volunteer.getClass()));
		DBObject query =new BasicDBObject();
		//唯一字段
		query.put("uid", volunteer.getUid());
		//找到要修改的对象
		DBObject findOne = collection.findOne(query);
		//修改时间变更
		volunteer.setUpdatetime(new Date());
		DBObject update=XiMethodUtil.method(volunteer,findOne);//要找其他字段原来的值，不然update会给你自动修改其他的值为null
		collection.update(query, update, true, true);//有的话修改，没有的话进行添加！第三个参数为true。
		map.put("status", 1);
		map.put("code", "");
		map.put("description", "更新成功");
		return map;
	}
	
	/**
	 * @author liuxiaoxi
	 * @time 2017年3月27日
	 * @param void
	 * @return List<volunteer>
	 * @describe 根据某个条件查询信息的列表post请求写法
	 */
	@ApiOperation("PSOT getModelByid~~~~~~~~~~~~~~~~~~~~~~~~~~~~测试成功")
	@RequestMapping(value = "/getVLModelByid", method = RequestMethod.POST)
	public Map<String, Object> getVLModelByid(@RequestParam("uid") String uid) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(null==uid||uid.length()==0){
			map.put("status", 0);
			map.put("code", "");
			map.put("description", "查询失败");
			return map;
		}
		Volunteer findOne = mongoTemplate.findOne(new Query(new Criteria("uid").is(uid)), Volunteer.class);
		map.put("status", 1);
		map.put("code", findOne);
		map.put("description", "查询成功");
		return map;
	}
	
	/**
	 * @author liuxiaoxi
	 * @time 2017年3月25日
	 * @param void
	 * @return List<volunteer>
	 * @describe 查询所有的信息
	 */
	@ApiOperation("getvolunteerList~~~~~~~~~~~~~~~~~~~~~~~~~~~~测试成功")
	@RequestMapping(value = "/getVLList", method = RequestMethod.GET)
	public Map<String, Object>  getVLList() {
		Map<String, Object> map = new HashMap<String, Object>();
		Query query=new Query(); 
		query.with(new Sort(Direction.DESC, "updatetime"));//按时间倒序排序  
		List<Volunteer> find = mongoTemplate.find(query, Volunteer.class);
		map.put("status", 1);
		map.put("code", "");
		map.put("list", find);
		map.put("description", "查询成功");
		return map;
	}
	
	/**
	 * @author liuxiaoxi
	 * @time 2017年3月27日
	 * @param void
	 * @return List<volunteer>
	 * @describe 根据某个条件查询信息的列表get请求写法
	 */
	@ApiOperation("GET getListByName~~~~~~~~~~~~~~~~~~~~~~~~~~~~测试成功")
	@RequestMapping(value = "/getVLById/{uid}", method = RequestMethod.GET)
	public Map<String, Object> getVLById(@RequestParam("uid") String uid) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(null==uid||uid.length()==0){
			map.put("status", 0);
			map.put("code", "");
			map.put("description", "查询失败");
			return map;
		}
		List<Volunteer> find = mongoTemplate.find(new Query(new Criteria("uid").is(uid)), Volunteer.class);
		map.put("status", 1);
		map.put("code", "");
		map.put("list", find);
		map.put("description", "查询成功");
		return map;
	}

	/**
	 * @author liuxiaoxi
	 * @time 2017年3月25日
	 * @param volunteer,skip
	 * @return List<volunteer>
	 * @describe 模糊查询
	 */
	@SuppressWarnings({ "unchecked", "deprecation" })
	@ApiOperation("getLikeVLPage~~~~~~~~~~~~~~~~~~~~~~~~~~~~测试成功")
	@RequestMapping(value = "/getVLLikePage", method = RequestMethod.POST)
	public PageModel<Volunteer> getVLLikePage(@ModelAttribute PageModel<Volunteer> page,@ModelAttribute Volunteer volunteer) throws ParseException {
		DBCollection collection = mongoTemplate.getCollection(mongoTemplate.getCollectionName(Volunteer.class));
		DBObject query=XiMethodUtil.likeMethod(volunteer);//改成模糊查询的条件
		DBObject tiaojian =new BasicDBObject();//累计时间大小查询
		query.put("invalid", "1");//逻辑删除未被删除的数据
		if (null!=volunteer.getExclusion()&&volunteer.getExclusion()!=""&&volunteer.getExclusion().length()>0) {
			if (volunteer.getExclusion().equals("1")||volunteer.getBusinessstime()==0) {//累计时间大于等于当前传进来的值 $gt:大于、$gte:大于等于、$lt:小于、$lte:小于等于
				tiaojian.put("$gte",volunteer.getBusinessstime());
				query.put("businessstime", tiaojian);
			}else if (volunteer.getExclusion().equals("0")) {//累计时间小于等于当前传进来的值
				tiaojian.put("$lte",volunteer.getBusinessstime());
				query.put("businessstime", tiaojian);
			}
		}
		DBCursor find =collection.find(query).skip((page.getPageNumber()-1)*page.getPageSize()).limit(page.getPageSize());//多条件分页查询
		//将返回集合变为对应实体类类型的集合！
		List<DBObject> array = find.toArray();
		JSONArray comJson = JSONArray.fromObject(JSONArray.fromObject(array.toString()));
		List<Volunteer> list = JSONArray.toList(comJson, Volunteer.class);
		page.setRows(list);//查到所有的列表
		page.setTotal(collection.find(query).count());//总数
		return page;
	}
}
